/*
 * comlan
 */

package com.comlan.installer;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.app.Service;
import android.content.Intent;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageParser;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.DisplayMetrics;
import android.widget.Toast;

public class UninstallerService extends Service {
	private static final String TAG = "installer";
    private final int UNINSTALL_COMPLETE = 1;
    public final static int SUCCEEDED=1;
    public final static int FAILED=0;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			 
			switch (msg.what) {
			case UNINSTALL_COMPLETE:
				Toast.makeText(UninstallerService.this, "uninstall "+ packageName +" success.", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};
	private String packageName;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
		public void packageDeleted(boolean succeeded) {
			Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
			msg.arg1 = succeeded ? SUCCEEDED : FAILED;
			mHandler.sendMessage(msg);
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();

	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		try{			
			packageName = intent.getStringExtra("packageName");
			PackageManager pm = getPackageManager();
			PackageDeleteObserver observer = new PackageDeleteObserver();
			getPackageManager().deletePackage(packageName, observer, 0);	
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
